#!/bin/bash

source $HOME/.bashrc && conda activate lxb39 && cd $PROJECTS/MoE-LLaVA

gpu_list="${CUDA_VISIBLE_DEVICES:-0}"
IFS=',' read -ra GPULIST <<< "$gpu_list"
CHUNKS=${#GPULIST[@]}

CONV="phi"
CKPT_NAME="moe-llava-clip-336-phi-2.7b-3rd-sft-moe-reproduce"
CKPT="$OUTPUTS/MoE-LLaVA/${CKPT_NAME}"
EVAL="eval_files"

mkdir -p ${EVAL}/scienceqa/answers/${CKPT_NAME}
for IDX in $(seq 0 $((${CHUNKS} - 1))); do
    deepspeed --include localhost:${GPULIST[${IDX}]} --master_port $((25334 + ${GPULIST[${IDX}]})) moellava/eval/model_vqa_science.py \
        --model-path ${CKPT} \
        --question-file ${EVAL}/scienceqa/llava_test_CQM-A.json \
        --image-folder ${EVAL}/scienceqa/images/test \
        --answers-file ${EVAL}/scienceqa/answers/${CKPT_NAME}/${CHUNKS}_${IDX}.jsonl \
        --single-pred-prompt \
        --num-chunks ${CHUNKS} \
        --chunk-idx ${IDX} \
        --temperature 0 \
        --conv-mode ${CONV} &
done
wait

output_file=${EVAL}/scienceqa/answers/${CKPT_NAME}/${CKPT_NAME}.jsonl
> "$output_file"
for IDX in $(seq 0 $((${CHUNKS} - 1))); do
    cat ${EVAL}/scienceqa/answers/${CKPT_NAME}/${CHUNKS}_${IDX}.jsonl >> "$output_file"
done

python3 moellava/eval/eval_science_qa.py \
    --base-dir ${EVAL}/scienceqa \
    --result-file ${EVAL}/scienceqa/answers/${CKPT_NAME}/${CKPT_NAME}.jsonl \
    --output-file ${EVAL}/scienceqa/answers/${CKPT_NAME}/${CKPT_NAME}_output.jsonl \
    --output-result ${EVAL}/scienceqa/answers/${CKPT_NAME}/${CKPT_NAME}_result.json

